package sys_serv

import (
	"strconv"
	"strings"
	"ubt-gin-admin/app/api/model/sys_mod"
	"ubt-gin-admin/app/pkg/db"
	"ubt-gin-admin/app/pkg/enums"
)

type sysConf struct{}

var SysConfServ = sysConf{}

// Add 新增配置
func (s *sysConf) Add(req *sys_mod.SysConfSaveReq) (string, string) {
	sqlStr := `INSERT INTO sys_config(conf_type, conf_name, conf_key, conf_val, remarks, states, add_time, upd_time) 
		VALUES ($1, $2, $3, $4, $5, 1, EXTRACT(epoch FROM NOW()), EXTRACT(epoch FROM NOW()))`
	return db.CudData(sqlStr, req.ConfType, req.ConfName, req.ConfKey, req.ConfVal, req.Remarks)
}

// Exists 判断是否存在
func (s *sysConf) Exists(req *sys_mod.SysConfExtVo) bool {
	sqlStr := "SELECT conf_id FROM sys_config WHERE states IN (1, 3) AND (conf_type=$1 OR conf_name=$2 OR conf_key=$3) AND conf_val=$4"
	param := make([]any, 0)
	param = append(param, req.ConfType)
	param = append(param, req.ConfName)
	param = append(param, req.ConfKey)
	param = append(param, req.ConfVal)
	if req.ConfId != "" {
		sqlStr += " AND conf_id!=$5"
		param = append(param, req.ConfId)
	}
	if _, _, data := db.GetOne(sqlStr, param...); len(data) == 0 {
		return false
	}
	return true
}

// Update 修改配置
func (s *sysConf) Update(req *sys_mod.SysConfSaveReq) (string, string) {
	sqlStr := `UPDATE sys_config SET conf_type=$1, conf_name=$2, conf_key=$3, conf_val=$4, remarks=$5, 
		upd_time=EXTRACT(epoch FROM NOW()) WHERE conf_id=$6`
	return db.CudData(sqlStr, req.ConfType, req.ConfName, req.ConfKey, req.ConfVal, req.Remarks, req.ConfId)
}

// Process 处理配置
func (s *sysConf) Process(req *sys_mod.SysConfProReq) (string, string) {
	var (
		param []any
		q     string
		n     = 1
	)
	param = append(param, req.States)
	idList := strings.Split(req.ConfIds, ",")
	for _, curId := range idList {
		n++
		q += "$" + strconv.Itoa(n) + ","
		param = append(param, curId)
	}
	q = q[:len(q)-1]
	sqlStr := "UPDATE sys_config SET states=$1, upd_time=EXTRACT(epoch FROM NOW()) WHERE conf_id IN (" + q + ")"
	return db.CudData(sqlStr, param...)
}

// One 获取配置详情
func (s *sysConf) One(confId string) (string, string, any) {
	sqlStr := "SELECT conf_id, conf_type, conf_name, conf_key, conf_val, remarks, states, add_time, upd_time FROM sys_config WHERE conf_id=$1"
	return db.GetOne(sqlStr, confId)
}

// Count 获取配置数量
func (s *sysConf) Count(req *sys_mod.SysConfPageReq) (string, string, any) {
	sqlStr := "SELECT COUNT(1) AS nums FROM sys_config WHERE states IN (1, 3)"
	param := make([]any, 0)
	var n = 0
	if req.Keywords != "" {
		sqlStr += " AND ("
		nameArr := strings.Split(req.Keywords, " ")
		for _, name := range nameArr {
			sqlStr += "conf_type LIKE '%'||$" + strconv.Itoa(n+1) + "||'%' OR " +
				"conf_name LIKE '%'||$" + strconv.Itoa(n+2) + "||'%' OR " +
				"conf_key LIKE '%'||$" + strconv.Itoa(n+3) + "||'%' OR " +
				"conf_val LIKE '%'||$" + strconv.Itoa(n+4) + "||'%' OR "
			param = append(param, name)
			param = append(param, name)
			param = append(param, name)
			param = append(param, name)
			n += 4
		}
		sqlStr = sqlStr[:len(sqlStr)-4]
		sqlStr += ")"
	}
	if code, msg, data := db.GetOne(sqlStr, param...); len(data) > 0 && data["nums"].(int64) > 0 {
		return code, msg, data["nums"]
	}
	return enums.NoData.Code, enums.NoData.Msg, 0
}

// Page 获取配置分页列表
func (s *sysConf) Page(req *sys_mod.SysConfPageReq) (string, string, any) {
	sqlStr := "SELECT conf_id, conf_type, conf_name, conf_key, conf_val, states FROM sys_config WHERE states IN (1, 3)"
	param := make([]any, 0)
	var n = 0
	if req.Keywords != "" {
		sqlStr += " AND ("
		nameArr := strings.Split(req.Keywords, " ")
		for _, name := range nameArr {
			sqlStr += "conf_type LIKE '%'||$" + strconv.Itoa(n+1) + "||'%' OR " +
				"conf_name LIKE '%'||$" + strconv.Itoa(n+2) + "||'%' OR " +
				"conf_key LIKE '%'||$" + strconv.Itoa(n+3) + "||'%' OR " +
				"conf_val LIKE '%'||$" + strconv.Itoa(n+4) + "||'%' OR "
			param = append(param, name)
			param = append(param, name)
			param = append(param, name)
			param = append(param, name)
			n += 4
		}
		sqlStr = sqlStr[:len(sqlStr)-4]
		sqlStr += ")"
	}
	sqlStr += " ORDER BY states, conf_type, conf_key LIMIT $" + strconv.Itoa(n+1) + " OFFSET $" + strconv.Itoa(n+2)
	param = append(param, req.Limit)
	param = append(param, req.Offset)
	return db.GetAll(sqlStr, param...)
}

// Select 获取配置下拉列表
func (s *sysConf) Select() (string, string, any) {
	sqlStr := "SELECT conf_id, conf_type, conf_name, conf_key, conf_val FROM sys_config WHERE states IN (1, 3)"
	return db.GetAll(sqlStr)
}
